import { reactive, inject } from "vue";
import { useProjectStore } from '../../../store/useProjectStore'

export const useProjectTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const projectTable = reactive(
        {
            total: 0,
            tableData: [],
            stakeholderList: [],
            documentList: [],
            queryCondition: {
                projectName: '',
            }
        }
    );

    const projectStore = useProjectStore();

    //查询
    const queryData = async (status) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.GetAllProject,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { projectName: projectTable.queryCondition.projectName,status:status },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectTable.tableData = response.data.data;
                projectTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //获取项目干系人
    const getProjectStakeholder = async (projectId, stakeholderType) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetProjectStakeholder,
                {
                    projectId: projectId,
                    stakeholderType: stakeholderType
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectTable.stakeholderList = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //获取项目文件
    const getProjectDoc = async (projectId, docType) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetProjectDocments,
                {
                    projectId: projectId,
                    docType: docType
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectTable.documentList = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.Create,
                projectStore.projectModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.Update,
                projectStore.projectModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新项目指定信息
    const updateProjectSpecificContent = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.UpdateProjectSpecificContent,
                projectStore.projectModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (projectId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.DeleteByProjectId,
                { 'projectId': projectId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //删除项目文件
    const deleteProjectDoc = async (objectId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.DeleteProjectDocByObjectId,
                { objectId: objectId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    return { projectTable, projectStore, queryData, getProjectStakeholder, getProjectDoc, addData, updateData, updateProjectSpecificContent, deleteData, deleteProjectDoc };
};